package org.ipb.controller.modul.kontakpegawai;

import org.ipb.model.KontakPegawai;
import org.ipb.model.Pegawai;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionJQGridCrud extends ActionSupport {
	private static final long serialVersionUID = -5933849517817526086L;
	private String id;
	private String oper;
	private String jenisKontak;
	private String kontak;
	private String pegawai;

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String detail() {

		return SUCCESS;
	}

	public String page() {
		return "page";
	}

	public String execute() {
		if (getOper().equals("edit")) {
			return update();
		} else if (getOper().equals("add")) {
			return insert();
		} else {
			return delete();
		}
	}

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();
		KontakPegawai object = new KontakPegawai();
		Pegawai pegawai = new Pegawai();
		pegawai.setId(Integer.valueOf(getPegawai()));

		object.setPegawai(pegawai);
		object.setJenisKontak(getJenisKontak());
		object.setKontak(getKontak());

		dao.insert(object);
		dao.close();
		return SUCCESS;
	}

	public String update() {
		DAO<Integer> dao = new DAO<Integer>();
		KontakPegawai object = (KontakPegawai) dao.find(KontakPegawai.class,
				Integer.valueOf(getId()));
		object.setJenisKontak(getJenisKontak());
		object.setKontak(getKontak());
		dao.update(object);
		dao.close();
		return SUCCESS;
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();
		KontakPegawai delete = (KontakPegawai) dao.find(KontakPegawai.class,
				Integer.valueOf(getId()));
		dao.delete(delete);
		dao.close();
		return SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPegawai() {
		return pegawai;
	}

	public void setPegawai(String pegawai) {
		this.pegawai = pegawai;
	}

	public String getKontak() {
		return kontak;
	}

	public void setKontak(String kontak) {
		this.kontak = kontak;
	}

	public String getJenisKontak() {
		return jenisKontak;
	}

	public void setJenisKontak(String jenisKontak) {
		this.jenisKontak = jenisKontak;
	}

}
